package com.example.WebTask1.Controller;

import com.example.WebTask1.Dao.StudentDao;
import com.example.WebTask1.Dao.StudentDaoImp;
import com.example.WebTask1.Enity.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServletMessage", urlPatterns = "/ServletMessage")
public class ServletMessage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        if (method.equals("findAll")){
            this.queryStudents(request,response);

        }else if (method.equals("insert")){
           this.insertStudent(request,response);


        }else if (method.equals("updateFind")){
            this.updateFind(request,response);

        }else if (method.equals("updateStudent")){
            this.updateStudent(request,response);
        }else if (method.equals("delete")){
            this.deleteStudent(request,response);
        }else {

        }

    }

    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.printf("updateStudent");
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String emil = request.getParameter("emil");
        int phone = Integer.parseInt(request.getParameter("phone"));
        String comment = request.getParameter("comment");
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setSex(sex);
        student.setBirthday(birthday);
        student.setEmil(emil);
        student.setPhone(phone);
        student.setComment(comment);
        System.out.println(student);
        StudentDao studentDao=new StudentDaoImp();
        Boolean aBoolean = studentDao.updateStudent(student);
        if (aBoolean){
            response.sendRedirect(request.getContextPath()+"/ServletMessage?method=findAll");
        }
    }

    private void updateFind(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String emil = request.getParameter("emil");
        int phone = Integer.parseInt(request.getParameter("phone"));
        String comment = request.getParameter("comment");
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setSex(sex);
        student.setBirthday(birthday);
        student.setEmil(emil);
        student.setPhone(phone);
        student.setComment(comment);
        request.setAttribute("student",student);
        request.getRequestDispatcher("updateMessage.jsp").forward(request,response);

    }

    private void insertStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String emil = request.getParameter("emil");
        int phone = Integer.parseInt(request.getParameter("phone"));
        System.out.println(name);
        String comment = request.getParameter("comment");
        Student student = new Student();
        student.setName(name);
        student.setSex(sex);
        student.setBirthday(birthday);
        student.setEmil(emil);
        student.setPhone(phone);
        student.setComment(comment);
        StudentDao studentDao=new StudentDaoImp();
        Boolean aBoolean = studentDao.insertStudent(student);
        if (aBoolean){
            response.sendRedirect(request.getContextPath()+"/ServletMessage?method=findAll");
        }
    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Student student=new Student();
        student.setId(id);
        StudentDao studentDao=new StudentDaoImp();
        Boolean aBoolean = studentDao.deleteStudent(student);
        if (aBoolean){
            response.sendRedirect(request.getContextPath()+"/ServletMessage?method=findAll");
        }

    }

    private void queryStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao studentDao=new StudentDaoImp();
        List<Student> list = studentDao.queryStudents();
        request.setAttribute("list",list);
        request.getRequestDispatcher("message.jsp").forward(request,response);
    }


}
